<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/8
 * Time: 13:46
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\GoodsModel;
use app\portal\model\PortalCategoryModel;
use think\Db;

class CommodityController extends AdminBaseController
{
    /*
     * 获取登录信息
     * */
    public function session()
    {
        $session=cmf_get_current_admin_id();
        $where['id']=$session;
        $store=DB::name('user')->where($where)->field('store_id')->find();
        $store_id=$store['store_id'];
        return $store_id;
    }
    //商品列表页面
    //下拉表的条件选择上  HTML页面中下拉框的name于数据表里面的类型相对应即可
    public function index()
    {
        /**搜索条件**/
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        $session = $this->session();
        $goodsname = $this->request->param('g_goodsname');
        $storeId = $this->request->param('storeid');        
        $where = array();
        if($session != 0)
        {
            $where['storeid']=$session;
        }
        $store_str=array();       
        if ($goodsname){
            $where['g_goodsname'] = ['like', "%$goodsname%"];
        }
        $cause=array();
        $cause['status']=1;
        if(isset($storeId) && !empty($storeId)){
            $where['storeid'] = $storeId;
            $cause['id']=$storeId;
        }else{
            $userList=get_user_list($uid);
            $uid_list_str=toStr($userList,'userid');
            if($uid_list_str){
                $uid_list_str.=','.$uid;
            }else{
                $uid_list_str=$uid;
            }
            $store_id_list=get_store_list($uid_list_str);
            if($store_id_list){
                $where['storeid']=array('in',$store_id_list);
                $cause['id']=array('in',$store_id_list);
            }else{
                $where['storeid']=array('in','');
                $cause['id']=array('in','');
            }
            $storeId=0;
        }
        $where['g_status']=['gt',-3];
        $goods = Db::name('Goods')->where($where)->order("g_id DESC")->paginate(10);
        if($goods->toArray());
        {

            foreach($goods as $k=>$v)
            {
                $data='';
                $data=$v;
                switch ($data['g_status'])
                {
                    case -1;
                         $data['status_name']='待审核';
                         break;
                    case 0;
                        $data['status_name']='商品下架';
                        break;
                    case 1;
                        $data['status_name']='商品上架';
                        break;
                    case -2;
                        $data['status_name']='未通过';
                        break;
                    default:
                        $data['status_name']='未知错误';
                }
                $goods->offsetSet($k,$data);

            }
        }
        $goods->appends(['g_goodsname'=>$goodsname,'storeid'=>$storeId]);
        // 获取分页显示
        $page = $goods->render();
        $pages=$goods->getCurrentPage();
        //获取店铺类型
        $storetype=DB::name('store')->where($cause)->select()->toarray();
        //店铺列表
        //判断是不是店长
        $storeInfo=DB::name('store')->where(array('ownerid'=>$uid,'status'=>1))->find();
        if($storeInfo){//店长登录
            $special=$storeInfo['special'];
            $isdianzhang=1;
        }else{
            $special=0;
            $isdianzhang=0;
        }
        $this->assign('special',$special);
        $this->assign('isdianzhang',$isdianzhang);
        $this->assign('role_type',$uinfo['role_type']);
        $this->assign('user',$session);
        $this->assign('g_goodsname',$goodsname);
        $this->assign('storeid',$storeId);
        $this->assign('stort',$storetype);
        $this->assign('pages',$pages);
        $this->assign("page", $page);
        $this->assign("data", $goods);
        return $this->fetch();
    }
    //编辑
    public function edit()
    {
        $id = $this->request->param("g_id");
        $storeid=$this->request->param('storeid');
        $g_goodsname=$this->request->param('g_goodsname');
        $data=Db::name('Goods')->where(array('g_id'=>$id))->find();
        //获取单位
        $unit = Db::name('Unit')->where(array('status'=>1))->select();
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();        
        //系列
        $brand = Db::name('brand')->where(array('status'=>1))->select();
        $this->assign("brand", $brand);
        $this->assign("unit", $unit);
        $this->assign("classify", $classify);        
        $this->assign("data", $data);
        $this->assign('g_goodsname',$g_goodsname);
        $this->assign('storeid',$storeid);
        return $this->fetch();
    }
    /*
    * 修改商品价格和名称
    * */
    public function editPost(){
        if ($this->request->isPost()) {
            $res  = $this->request->param();
            $storeid=$res['storeid'];
            $g_goodsname=$res['g_goodsname'];
            if (!empty($res['photo_names']) && !empty($res['photo_urls'])) {
                $res['post']['more']['photos'] = [];
                foreach ($res['photo_urls'] as $key => $url) {
                    $photoUrl = cmf_asset_relative_url($url);
                    array_push($res['post']['more']['photos'], ["url" => $photoUrl, "name" => $res['photo_names'][$key]]);
                }
            }
            $data['g_goodsname']=$res['options']['g_goodsname'];
            if(isset($res['photo_urls'][0])){
                $data['g_pic']=$res['photo_urls'][0];
            }
            $data['g_goodsname']=$res['options']['g_goodsname'];
            $data['g_price']=$res['options']['price'];
            $data['g_vipprice']=$res['options']['vipprice'];
            $data['g_status']=-1;
            $data['g_utime']=time();
            $where['g_id']=$res['g_id'];
            $re=Db::name('goods')->where($where)->update($data);
            if($re){
                $this->success("修改成功！",url('blog/commodity/index',array('storeid'=>$storeid,"g_goodsname"=>$g_goodsname)));
            }else{
                $this->error("修改失败！");
            }
        }
    }
    
    
    
    //删除商品
    public function delete()
    {
        $id = $this->request->param("g_id");
        $info = Db::name('Goods')->where(['g_id' => $id])->find();
        if ($info){
            $status = Db::name('Goods')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", url('Commodity/index'));
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("该商品不存在！");
        }
    }
    /*
     * 状态为-1和-2的不能修改
     *
     * */
    public function upStatus()
    {
        $session=$this->session();
        $intId     = $this->request->param("g_id");
        $intStatus = $this->request->param("g_status");
        $storeid=$this->request->param('storeid');
        $page=$this->request->param('page');
        $g_gooodsname=$this->request->param('g_goodsname');
        if (empty($intId)) {
            $this->error(lang("请选择要修改数据"));
        }
        $where['g_id']=$intId;
        $status=DB::name('goods')->where($where)->find();
        if($session!=0)
        {
            if($status['g_status']<0)
            {
                $this->error(lang("商品正在审核"));
            }
        }
        $data['g_status']=$intStatus;
        $status=DB::name('goods')->where($where)->update($data);
          if($status)
          {
              if($page == 1)
              {
                  $this->success('修改成功',url('blog/commodity/index',array('storeid'=>$storeid,"g_goodsname"=>$g_gooodsname)));
              }else{
                  $this->success('修改成功');
              }
          }else{
              $this->error("修改失败");
          }
    } 
    //新增
    public function addgoods()
    {
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        //系列
        $brand = Db::name('brand')->where(array('status'=>1))->select();
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();
        ///$this->assign("storeid", session());
        $this->assign("brand", $brand);
        $this->assign("classify", $classify);
        return $this->fetch();
    }
}